/*
 * Copyright (C) 2025 Tsingteng MicroSystem
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "nfc_osi_api.h"
#include "nfc_log_api.h"

/**
 * @brief 原子读
 * @param atomic 要操作的原子变量对象
 * @return 原子变量的值
 */
uint32_t osi_atomic_read(tATOMIC *atomic)
{
    return atomic_load(atomic);
}

/**
 * @brief 原子写
 * @param atomic  要操作的原子变量对象
 * @param val     给原子变量更新的值
 */
void osi_atomic_write(tATOMIC *atomic, uint32_t val)
{
    atomic_store(atomic, val);
}

/**
 * @brief 原子先读再写值
 * @param atomic 要操作的原子变量对象
 * @param val    给原子变量更新的值
 * @return       原子变量更新之前的值
 */
uint32_t osi_atomic_read_then_write(tATOMIC *atomic, uint32_t val)
{
    return atomic_exchange(atomic, val);
}

/**
 * @brief 原子加
 * @param atomic 要操作的原子变量对象
 * @param val    给原子变量值相加的值
 */
void osi_atomic_add(tATOMIC *atomic, uint32_t val)
{
    atomic_fetch_add(atomic, val);
}

/**
 * @brief 原子减
 * @param atomic 要操作的原子变量对象
 * @param val    给原子变量值相减的值
 */
void osi_atomic_sub(tATOMIC *atomic, uint32_t val)
{
    atomic_fetch_sub(atomic, val);
}

/**
 * @brief 原子与
 * @param atomic 要操作的原子变量对象
 * @param val    给原子变量值做与操作的值
 */
void osi_atomic_and(tATOMIC *atomic, uint32_t val)
{
    atomic_fetch_and(atomic, val);
}

/**
 * @brief 原子或
 * @param atomic 要操作的原子变量对象
 * @param val    给原子变量值做或操作的值
 */
void osi_atomic_or(tATOMIC *atomic, uint32_t val)
{
    atomic_fetch_or(atomic, val);
}

/**
 * @brief 原子异或
 * @param atomic 要操作的原子变量对象
 * @param val    给原子变量值做异或操作的值
 */
void osi_atomic_xor(tATOMIC *atomic, uint32_t val)
{
    atomic_fetch_xor(atomic, val);
}
